package com.yq.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yq.reggie.common.BaseContext;
import com.yq.reggie.mapper.ShoppingCartMapper;
import com.yq.reggie.pojo.ShoppingCart;
import com.yq.reggie.service.ShoppingCartService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    /**
     *  添加菜品或套餐到购物车
     * @param shoppingCart
     */
    @Override
    public void add(ShoppingCart shoppingCart) {
        //判断购物车是否有这个商品:没有则新建，有则修改number属性为number+1
        //sql: select * from shopping_cart where user_id=?,name=?,dish_flavor=?
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        queryWrapper.eq(ShoppingCart::getName,shoppingCart.getName());
        queryWrapper.eq(shoppingCart.getDishFlavor()!=null,ShoppingCart::getDishFlavor,shoppingCart.getDishFlavor());
        ShoppingCart shoppingCart2 = this.getOne(queryWrapper);
        if(shoppingCart2==null){
            //向shopping_cart添加数据
            //sql: insert into shopping_cart(id,name,image,user_id,dish_id,dish_flavor,number,amount,create_time) values (?,?,?,?,?,?,?)
            shoppingCart.setUserId(userId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            this.save(shoppingCart);
        }else {
            //修改shopping_cart表number属性
            //sql: update shopping_cart set number=? where id=?;
            shoppingCart2.setNumber(shoppingCart2.getNumber()+1);
            this.updateById(shoppingCart2);
        }
    }

    /**
     * 减少购物车里的菜品或套餐
     * @param shoppingCart
     */
    @Override
    public void sub(ShoppingCart shoppingCart) {
        //根据dish_id或setmeal_id和user_id查询shopping_cart表
        //sql: select * from shopping_cart where user_id=? and dish_id=?/setmeal_id=?
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        Long userId = BaseContext.getCurrentId();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        ShoppingCart shoppingCart2 = this.getOne(queryWrapper);
        if(shoppingCart2.getNumber()>1){
            //修改shopping_cart表number=number-1
            //sql: update shopping_cart set number=? where id=?
            shoppingCart2.setNumber(shoppingCart2.getNumber() - 1);
            this.updateById(shoppingCart2);
        }else{
            //根据id删除shopping_cart表中数据
            //sql: delete from shopping_cart where id=?
            this.removeById(shoppingCart2);
        }
    }

}
